package com.kgis.fm.common.manager
{
	import com.kgis.fm.flex.model.constant.LayerTypeConstant;
	import com.kgis.fm.flex.model.term.BusinessLineStyle;
	import com.kgis.fm.flex.model.term.BusinessPointStyle;
	import com.kgis.fm.flex.model.term.BusinessPolygonStyle;
	import com.kgis.fm.flex.model.term.BusinessStyleRule;
	import com.kgis.fm.flex.model.term.BusinessTextStyle;
	import com.kgis.fm.flex.model.term.LegendEntry;
	import com.kgis.map.Map;
	import com.kgis.map.rule.Rule;
	import com.kgis.map.style.LabelStyle;
	import com.kgis.map.style.PointStyle;
	import com.kgis.map.style.PolygonStyle;
	import com.kgis.map.style.PolylineStyle;
	import com.kgis.map.style.StyleRule;
	
	import flash.utils.Dictionary;
	
	import mx.collections.ArrayCollection;

	/**
	 * StyleRule构造器
	 * */
	public class StyleRuleManager
	{
		public function StyleRuleManager()
		{
		}

		public static function getStyleRule(legendEntry:LegendEntry, kgisMap:Map):Rule
		{
			if(!legendEntry){
				return null;
			}
			var styleRuleList:ArrayCollection=MetaDataManager.getStyleRuleByLegendEntryId(legendEntry.leno);
			var result:Rule=new Rule();
			switch (legendEntry.layerType)
			{
				case LayerTypeConstant.LAYERTYPE_POINT:
					result=buildPointStyleRule(styleRuleList, kgisMap);
					break;
				case LayerTypeConstant.LAYERTYPE_POLYLINE:
					result=buildLineStyleRule(styleRuleList);
					break;
				case LayerTypeConstant.LAYERTYPE_POLYGON:
					result=buildPolygonStyleRule(styleRuleList);
					break;
				case LayerTypeConstant.LAYERTYPE_TEXT:
					result=buildTextStypeRule(styleRuleList);
					break;
			}
			return result;
		}

		private static function buildPointStyleRule(styleRuleList:ArrayCollection, kgisMap:Map):Rule
		{
			var styleRule:Rule=null;
			for each (var pointStyleRule:BusinessStyleRule in styleRuleList){
				var businessPointStyle:BusinessPointStyle=MetaDataManager.getGeometryStyleById(pointStyleRule.sno,LayerTypeConstant.LAYERTYPE_POINT) as BusinessPointStyle;
				if (businessPointStyle)
				{
					var pointStyle:PointStyle=new PointStyle();
					pointStyle.alignment=businessPointStyle.alignment;
					pointStyle.alpha=businessPointStyle.alpha;
					pointStyle.color=businessPointStyle.color;
					pointStyle.rotation=businessPointStyle.angle as String;
					pointStyle.scale=businessPointStyle.scale.split(",");
					if (kgisMap.symbolDic[businessPointStyle.symbolName])
					{
						pointStyle.symbolClass=kgisMap.symbolDic[businessPointStyle.symbolName] as Class;
					}
					if (!styleRule)
					{
						styleRule=new Rule();
						var ruleDictionary:ArrayCollection=new ArrayCollection();
						styleRule.expressionFilterDic=ruleDictionary;
					}
					var sr:StyleRule=new StyleRule();
					if (!pointStyleRule.filter)
					{
						sr.filter="1==1";
						//							defaultGeoStyle=pointStyle;
					}
					else
					{
						sr.filter=pointStyleRule.filter;
					}
					sr.style=pointStyle;
					ruleDictionary.addItem(sr);
				}
			}
			return styleRule;
		}

		private static function buildLineStyleRule(styleRuleList:ArrayCollection):Rule
		{
			var styleRule:Rule=null;
			for each (var lineStyleRule:BusinessStyleRule in styleRuleList){
				var businessLineStyle:BusinessLineStyle=MetaDataManager.getGeometryStyleById(lineStyleRule.sno, LayerTypeConstant.LAYERTYPE_POLYLINE) as BusinessLineStyle;
				if (businessLineStyle)
				{
					var polylineStyle:PolylineStyle=new PolylineStyle();
					polylineStyle.alpha=businessLineStyle.alpha;
					polylineStyle.color=businessLineStyle.color;
					polylineStyle.width=businessLineStyle.width.split(",");
					polylineStyle.dashMode=businessLineStyle.dashMode;
					if (!styleRule)
					{
						styleRule=new Rule();
						var ruleDictionary:ArrayCollection=new ArrayCollection();
						styleRule.expressionFilterDic=ruleDictionary;
					}
					
					var sr:StyleRule=new StyleRule();
					
					if (!lineStyleRule.filter)
					{
						sr.filter="1==1";
						
						//ruleDictionary["1==1"]=polylineStyle;
					}
					else
					{
						//ruleDictionary[lineStyleRule.filter]=polylineStyle;
						sr.filter=lineStyleRule.filter
					}
					sr.style=polylineStyle;
					ruleDictionary.addItem(sr);
				}
			}
			return styleRule;
		}

		private static function buildPolygonStyleRule(styleRuleList:ArrayCollection):Rule
		{
			var styleRule:Rule=null;
			for each (var polygonStyleRule:BusinessStyleRule in styleRuleList){
				var businessPolygonStyle:BusinessPolygonStyle=MetaDataManager.getGeometryStyleById(polygonStyleRule.sno, LayerTypeConstant.LAYERTYPE_POLYGON) as BusinessPolygonStyle;
				if (businessPolygonStyle)
				{
					var polygonStyle:PolygonStyle=new PolygonStyle();
					polygonStyle.edgeAlpha=businessPolygonStyle.edgeAlpha;
					polygonStyle.edgeColor=businessPolygonStyle.edgeColor;
					polygonStyle.edgeWidth=businessPolygonStyle.edgeWidth.split(",");
					polygonStyle.fillAlpha=businessPolygonStyle.fillAlpha;
					polygonStyle.fillColor=businessPolygonStyle.fillColor;
					
					if (!styleRule)
					{
						styleRule=new Rule();
						var ruleDictionary:ArrayCollection=new ArrayCollection();
						styleRule.expressionFilterDic=ruleDictionary;
					}
					var sr:StyleRule=new StyleRule();
					if (!polygonStyleRule.filter)
					{
						sr.filter="1==1";
//						defaultGeoStyle=polygonStyle;
					}
					else
					{
						sr.filter=polygonStyleRule.filter;
					}
					sr.style=polygonStyle;
					ruleDictionary.addItem(sr);
				}
			}
			return styleRule;
		}
		private static function buildTextStypeRule(styleRuleList:ArrayCollection):Rule{
			var styleRule:Rule=null;
			for each (var textStyleRule:BusinessStyleRule in styleRuleList){
				var businessLabelStyle:BusinessTextStyle=MetaDataManager.getGeometryStyleById(textStyleRule.sno,LayerTypeConstant.LAYERTYPE_TEXT) as BusinessTextStyle;
				if (businessLabelStyle)
				{
					var labelStyle:LabelStyle=new LabelStyle();
					labelStyle.alignment=businessLabelStyle.alignment;
					labelStyle.alpha=businessLabelStyle.alpha;
					labelStyle.color=businessLabelStyle.color;
					labelStyle.font=businessLabelStyle.font;
					labelStyle.rotation=businessLabelStyle.angle as String;
//					labelStyle.size=[businessLabelStyle.size];
					//labelStyle.size = gmLabelStyle.fontSize.split(",");;
					labelStyle.xoffset=businessLabelStyle.offsetX;
					labelStyle.yoffset=businessLabelStyle.offsetY;
					
					if (!styleRule)
					{
						styleRule=new Rule();
						var ruleDictionary:ArrayCollection=new ArrayCollection();
						styleRule.expressionFilterDic=ruleDictionary;
					}
					var sr:StyleRule=new StyleRule();
					if (!textStyleRule.filter)
					{
						sr.filter="1==1";
						//ruleDictionary["1==1"]=labelStyle;
//						defaultLabelStyle=labelStyle;
					}
					else
					{
						sr.filter=textStyleRule.filter;
						//ruleDictionary[textStyleRule.filter]=labelStyle;
					}
					sr.style=labelStyle;
					ruleDictionary.addItem(sr);
				}
			}
			return styleRule;
		}
	}
}